<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        //获取商家ID

       $id = session()->get('shopInfo')->shop_id;
       //dd($id);
        //显示商家全部订单
        $orderInfo = DB::table('order')
                        ->where('order.shop_id',$id)
                        ->join('goods','order.goods_id','=','goods.goods_id')
                        ->join('users','order.uid','=','users.id')
                        ->select('order.*','goods.*','users.name')
                        ->orderBy('order_time','desc')
                        ->get();
       // dd($orderInfo);
        //显示未发货订单
         $paid = DB::table('order')
                        ->where(['order.shop_id'=>$id,'order_status'=>0,'refund'=>0])
                        ->join('goods','order.goods_id','=','goods.goods_id')
                        ->join('users','order.uid','=','users.id')
                        ->select('order.*','goods.*','users.name')
                        ->orderBy('order_time','desc')
                        ->get();
        //显示未付款订单
        $wait_pay = DB::table('order')
                        ->where(['order.shop_id'=>$id,'order_status'=>4])
                        ->join('goods','order.goods_id','=','goods.goods_id')
                        ->join('users','order.uid','=','users.id')
                        ->select('order.*','goods.*','users.name')
                        ->orderBy('order_time','desc')
                        ->get();

         //获取未确认收货订单
        $order_send = DB::table('order')
                        ->where(['order.shop_id'=>$id,'order_status'=>1,'refund'=>0])
                        ->join('goods','order.goods_id','=','goods.goods_id')
                        ->join('users','order.uid','=','users.id')
                        ->select('order.*','goods.*','users.name')
                        ->orderBy('order_time','desc')
                        ->get();
        //获取待评价订单
        $order_done = DB::table('order')
                        ->where(['order.shop_id'=>$id,'order_status'=>2,'refund'=>0])
                        ->join('goods','order.goods_id','=','goods.goods_id')
                        ->join('users','order.uid','=','users.id')
                        ->select('order.*','goods.*','users.name')
                        ->orderBy('order_time','desc')
                        ->get();

        $status = [];
        foreach($orderInfo as $v){
             //判断收货状态
            switch($v->order_status){
                case 0:
                $a ='买家已付款';
                break;
                case 1:
                $a = '卖家已发货';
                break;
                case 2:
                $a = '待评价';
                break;
                case 3:
                $a = '交易成功';
                break;
                case 4:
                $a = '未付款';
                break;
            }

           // var_dump($v->status);
           // echo '<br>';

            //判断是否在退货
            switch($v->refund){
                case 1:
                $a = '退款中';
                break;
                case 2:
                $a = '退款完成';
                break;
                case 3:
                $a = '退款失败';
                break;
            }
            $status[$v->id] = $a;
        }
        return view('home.shop_order')->with('orderInfo',$orderInfo)
                                      ->with('status',$status)
                                      ->with('order_paid',$paid)
                                      ->with('order_send',$order_send)
                                      ->with('wait_pay',$wait_pay)
                                      ->with('order_done',$order_done);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //发货操作
    public function orderSend($order_num,$goods_id)
    {
        //获取商家id
        $id = session()->get('shopInfo')->shop_id;

        //修改订单收货状态
        $res = DB::table('order')
                ->where([
                    'shop_id'=>$id ,
                    'order_num'=>$order_num ,
                    'goods_id'=>$goods_id
                    ])
                ->update(['order_status'=>1]);
        return redirect('shopOrder');
    }

    public function showRefund()
    {
        //获取商家ID
        $id = session()->get('shopInfo')->shop_id;
        //显示商家全部退货订单
        $refundInfo = DB::table('order')->where('order.shop_id',$id)
                                       ->whereIn('refund',[1,2,3])
                                       ->join('goods','order.goods_id','=','goods.goods_id')
                                       ->join('users','order.uid','=','users.id')
                                       ->join('refund','order.id','=','refund.order_id')
                                       ->select('order.*','goods.*','users.name','refund.*')
                                       ->orderBy('refund_time','desc')
                                       ->paginate(10);;
       // dd($refundInfo);
        $status = [];
        $refund_reason = [] ;
        foreach($refundInfo as $v){
            //转换退款原因
             switch($v->refund_reason){
                case 0:
                $reason = '不想要了';
                break;
                case 1:
                $reason = '买错了';
                break;
                case 2:
                $reason = '没收到货';
                break;
                case 3:
                $reason = '与说明不符';
                break;
            }
        $refund_reason[$v->id] = $reason;


            //判断退货状态
            switch($v->refund){
                case 1:
                $a = '退款中';
                break;
                case 2:
                $a = '退款完成';
                break;
                case 3:
                $a = '退款失败';
                break;
            }
            $status[$v->id] = $a;
        }
        return view('home.shop_refund')->with('refundInfo',$refundInfo)
                                       ->with('status',$status)
                                       ->with('reason',$refund_reason);
    }

    
    // 同意退款
    public function doRefund($order_num,$order_id)
    {
       // echo $order_num;
       // echo '<br>';
       // echo $order_id;
       // die;

        //获取商家ID
        $id = session()->get('shopInfo')->shop_id;
        $res = DB::table('order')
                ->where([
                    'shop_id'=>$id ,
                    'order_num'=>$order_num ,
                    'id'=>$order_id
                    ])//->first();
                ->update(['refund'=>2]);
       // dd($res);
        return redirect('showRefund');
    }

}
